package com.elric.mall.controller;

import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.dto.FundingInvestmentDto;
import com.elric.mall.dto.ProcessInfoDto;
import com.elric.mall.dto.ProjectBaseInfoDto;
import com.elric.mall.model.UmsAdmin;
import com.elric.mall.model.ZgProjectEntityNew;
import com.elric.mall.service.UserService;
import com.elric.mall.service.ZgProjectEntityNewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * 实现功能：战规-项目信息控制层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "ZgProjectEntityNewController", description = "规划项目信息接口")
@RequestMapping("/zgProject")
@RestController
public class ZgProjectEntityNewController extends BaseController {

    @Autowired
    ZgProjectEntityNewService zgProjectEntityNewService;

    @Autowired
    private UserService userService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ZgProjectEntityNew zgProjectEntityNew) {
        int count = zgProjectEntityNewService.create(zgProjectEntityNew);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody ZgProjectEntityNew zgProjectEntityNew) {
        int count = zgProjectEntityNewService.update(zgProjectEntityNew);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("基本信息保存")
    @RequestMapping(value = "/saveBaseInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult saveBaseInfo(@RequestBody ProjectBaseInfoDto projectBaseInfoDto, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        int count = zgProjectEntityNewService.saveBaseInfo(projectBaseInfoDto, umsAdmin);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("经费投入信息保存")
    @RequestMapping(value = "/saveFundingInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult saveFundingInfo(@RequestBody @Valid FundingInvestmentDto fundingInvestmentDto, BindingResult bindingResult) {
        int count = zgProjectEntityNewService.saveFundingInfo(fundingInvestmentDto);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据项目id、创建人id查询经费投入信息")
    @RequestMapping(value = "/findFundingInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findFundingInfo(@RequestParam(value = "id") String id, @RequestParam(value = "createId") String createId) {
        return CommonResult.success(zgProjectEntityNewService.findFundingInfo(id, createId));
    }

    @ApiOperation("项目进程信息保存")
    @RequestMapping(value = "/saveProcessInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult saveProcessInfo(@RequestBody @Valid ProcessInfoDto processInfoDto, BindingResult bindingResult) {
        int count = zgProjectEntityNewService.saveProcessInfo(processInfoDto);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据项目id、创建人id查询项目进程信息")
    @RequestMapping(value = "/findProcessInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findProcessInfo(@RequestParam(value = "id") String id, @RequestParam(value = "createId") String createId) {
        return CommonResult.success(zgProjectEntityNewService.findProcessInfo(id, createId));
    }

    @ApiOperation("根据ID获取项目详情")
    @RequestMapping(value = "/findOne", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ZgProjectEntityNew> findOne(@RequestParam(value = "id") String id, @RequestParam(value = "createId") String createId) {
        ZgProjectEntityNew ZgProjectEntityNew = zgProjectEntityNewService.findById(id, createId);
        return CommonResult.success(ZgProjectEntityNew);
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "id") String id, @RequestParam(value = "createId") String createId) {
        int count = zgProjectEntityNewService.delete(id, createId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<ZgProjectEntityNew>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestBody ZgProjectEntityNew zgProjectEntityNew) {
        List<ZgProjectEntityNew> menuList = zgProjectEntityNewService.list(pageSize, pageNum, zgProjectEntityNew);
        return CommonResult.success(CommonPage.restPage(menuList));
    }
}
